package com.glasgowrax.domain.directions;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.glasgowrax.domain.LatLngBounds;

public class Route {
	
	private String summary;
	private List<Leg> legs;
	private String polyline;
	private LatLngBounds bounds;
	private String copyrights;
	private List<String> warnings;
	
	public Route(JSONObject json) {
		
		try {
			summary = (json.has("summary")) ? (String) json.get("summary") : "";
			
			if (json.has("overview_polyline")) {
				JSONObject jsonPolyline = json.getJSONObject("overview_polyline");
				polyline = ((jsonPolyline.has("points")) ? (String) jsonPolyline.get("points") : "");
			}
			
			if (json.has("bounds")) {
				JSONObject jsonBounds = json.getJSONObject("bounds");
				if (jsonBounds.has("northeast") && jsonBounds.has("southwest")) {
					JSONObject jsonNE = jsonBounds.getJSONObject("northeast");
					JSONObject jsonSW = jsonBounds.getJSONObject("southwest");
					if (jsonNE.has("lat") && jsonNE.has("lng") && jsonSW.has("lat") && jsonSW.has("lng")) {
						bounds = new LatLngBounds(jsonSW.getDouble("lat"), jsonSW.getDouble("lng"), jsonNE.getDouble("lat"), jsonNE.getDouble("lng"));
					}
				}
			}
			
			copyrights = (json.has("copyrights")) ? (String) json.get("copyrights") : "";
			
			warnings = new ArrayList<String>();
			if (json.has("warnings")) {
				JSONArray jsonWarnings = json.getJSONArray("warnings");
				for (int i=0; i<jsonWarnings.length(); i++) {
					warnings.add((String) jsonWarnings.get(i));
				}
			}
			
			legs = new ArrayList<Leg>();
			if (json.has("legs")) {
				JSONArray jsonLegs = json.getJSONArray("legs");
				for (int i=0; i<jsonLegs.length(); i++) {
					legs.add(new Leg(jsonLegs.getJSONObject(i)));
				}
			}					
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public List<Leg> getLegs() {
		return legs;
	}

	public void setLegs(List<Leg> legs) {
		this.legs = legs;
	}

	public String getPolyline() {
		return polyline;
	}

	public void setPolyline(String polyline) {
		this.polyline = polyline;
	}

	public LatLngBounds getBounds() {
		return bounds;
	}

	public void setBounds(LatLngBounds bounds) {
		this.bounds = bounds;
	}

	public String getCopyrights() {
		return copyrights;
	}

	public void setCopyrights(String copyrights) {
		this.copyrights = copyrights;
	}

	public List<String> getWarnings() {
		return warnings;
	}

	public void setWarnings(List<String> warnings) {
		this.warnings = warnings;
	}
	
	
	
}
